% ВСТАВКА КАРТИНОК
\RequirePackage[dvips]{graphics}
\RequirePackage[dvips]{color}
\graphicspath{{pics/}{figs/}}

% РАЗМЕРЫ ОБРАМЛЕНИЯ
% отступ между левым краем рисунка и подписью
\newdimen\picindent@skip\picindent@skip=0mm
% расстояние между рисунком и подписью
\newdimen\piccaption@kern\piccaption@kern=1.5mm
% между рисунком и обтекающим текстом
\newdimen\pictext@kern\pictext@kern=4mm
% между рисунком и полями
\newdimen\picside@kern\picside@kern=0mm
% поле над рисунком
\newdimen\pictop@kern\pictop@kern=3mm
% поле под рисунком
\newdimen\picbottom@kern\picbottom@kern=3mm

\newbox\picbox@tmpx
\newdimen\picbox@width
\newdimen\picbox@tmpd
\newdimen\putbox@short
\newcount\putbox@lines
\newcount\putbox@remlines
\newcount\putbox@pos
\newcount\pre@after
\newdimen\pre@hang

% временные боксы для хранения картинок
\newbox\picaa\newbox\picab\newbox\picac

% шрифты для номера рисунка и для подписи
\def\pic@fontsize{\small}
\def\tag@font{\normalfont}
\def\cap@font{\normalfont}
\def\fig@name{\IeC{\CYRR}\IeC{\cyri}\IeC{\cyrs}}

\def\picref#1{\textrm{\IeC{\cyrr}\IeC{\cyri}\IeC{\cyrs}.\,\protect\ref{#1}}}

% команды для проверки типа подрисуночной надписи
\newif\if@piccaption
\newif\if@picnumber
\def\captiontest#1#2@@{\def\tmpa{#1}\def\tmpc{*}\ifx\tmpa\tmpc\global\@picnumberfalse%
    \gdef\pic@caption{#2}\ifx\pic@caption\empty\global\@piccaptionfalse\fi%
    \ifx\pic@caption\space\global\@piccaptionfalse\fi\fi}

% команда для сборки бокса с картинкой и подписью
\def\picbox[#1,#2,#3,#4]#5#6{\vbox{\pic@fontsize\def\pic@caption{#6}\@piccaptiontrue\@picnumbertrue%
    \ifx\pic@caption\space\def\pic@caption{}\fi\ifx\pic@caption\empty\else\setbox\picbox@tmpx=\hbox{\captiontest#6@@}\fi%
    \setbox\picbox@tmpx=\hbox{\protect\includegraphics{#5}}\picbox@tmpd=\wd\picbox@tmpx%
    \advance\picbox@tmpd by#2\kern#3\hbox to\picbox@tmpd{\kern#1\unhbox\picbox@tmpx}\if@piccaption%
    \kern\piccaption@kern\kern#4\vbox{\hsize\picbox@tmpd\hbox to\hsize{\hfill%
    \if@picnumber\refstepcounter{figure}\protect\label{#5}%
    {\tag@font\fig@name.\,\thefigure\kern2mm}\fi\pic@caption\hfill}}\fi}}%

% упрощенная команда для вставки картинки по центру
\def\centerpic#1#2{\vbox{\refstepcounter{figure}\protect\label{#1}%
\kern1ex\hbox to\textwidth{\hss\protect\includegraphics{#1}\hss}\kern-0.7ex%
\hbox to\textwidth{\hss\vbox{\hsize=.8\textwidth\begin{center}\pic@fontsize%
\IeC{\CYRR}\IeC{\cyri}\IeC{\cyrs}\,\thefigure%
\kern1em #2\end{center}}\hss}\kern-1ex}}

% команда для вставки бокса в текст
\def\putbox[#1][#2,#3]#4{\setbox\picbox@tmpx=\hbox{#4}\picbox@tmpd=\wd\picbox@tmpx%
    \advance\picbox@tmpd by\pictext@kern\advance\picbox@tmpd by\picside@kern%
    \putbox@pos=3\ifx#1c\putbox@pos=0\fi\ifx#1l\putbox@pos=1\fi%
    \ifx#1r\putbox@pos=2\fi\ifx#1t\putbox@pos=0\fi\ifx#1b\putbox@pos=0\fi%
    \ifx#1i\ifodd\@ne\relax\putbox@pos=2\else\putbox@pos=1\fi\fi%
    \ifx#1o\ifodd\@ne\relax\putbox@pos=1\else\putbox@pos=2\fi\fi\relax%
    \ifcase\putbox@pos%
         \setbox\picbox@tmpx=\vbox{\kern\ifx#1t0pt\else\pictop@kern\fi\hbox to\columnwidth{\hss\box\picbox@tmpx\hss}%
                    \kern\ifx#1b0pt\else\picbottom@kern\fi}%%
         \ifhmode\vadjust{\vskip 0mm plus1mm minus 0.5mm\box\picbox@tmpx\vskip 0mm plus1mm minus 0.5mm}%
             \else\vskip 0mm plus1mm minus 0.5mm\box\picbox@tmpx\vskip 0mm plus1mm minus 0.5mm\fi%
    \or\freeplace{\picbox@tmpd}{#2}{#3}%
         \setbox\picbox@tmpx=\vbox to0pt{\kern\pictop@kern\ifnum#2=0\kern-\baselineskip\fi%
                  \hbox to0pt{\kern\picside@kern\box\picbox@tmpx}}%
         \ifhmode\vadjust{\kern-\dp0\box\picbox@tmpx}\else\kern-\baselineskip\box\picbox@tmpx\fi%
    \or\freeplace{-\picbox@tmpd}{#2}{#3}%
         \setbox\picbox@tmpx=\vbox to0pt{\kern\pictop@kern\ifnum#2=0\kern-\baselineskip\fi%
                  \hbox to\columnwidth{\hss\box\picbox@tmpx\kern\picside@kern}}%
         \ifhmode\vadjust{\kern-\dp\picbox@tmpx\box\picbox@tmpx}\else\kern-\baselineskip\box\picbox@tmpx\fi%
    \else\@latex@error{Putbox: Wrong position argument. Russian "S"?}\fi}

\def\freeplace#1#2#3{\hangindent=#1\hangafter=#2\putbox@remlines=#3%
    \advance\putbox@remlines by#2\putbox@short=\columnwidth%
    \def\picbox@one{ 0pt \the\columnwidth}\putbox@lines=0%
    \ifdim#1>0pt\advance\putbox@short by-#1\def\picbox@two{ #1 \the\putbox@short}%
    \else\advance\putbox@short by#1\def\picbox@two{0pt \the\putbox@short}\fi%
    {\xdef\line@one{}\loop\advance\putbox@lines by 1\ifnum\putbox@lines<#2%
          \xdef\line@one{\line@one\picbox@one}\repeat}%
    {\xdef\line@two{}\loop\advance\putbox@lines by 1\ifnum\putbox@lines<#3%
          \xdef\line@two{\line@two\picbox@two}\repeat}%
    \putbox@lines=#2\advance\putbox@lines by#3\advance\putbox@lines by 1%
    \parshape \putbox@lines \line@one \line@two \picbox@one%
    \def\par{\sloppy\picbox@width=\the\hangindent%
             \endgraf\advance\putbox@remlines by-\the\prevgraf%
             \hangafter=-\the\putbox@remlines\hangindent=\picbox@width%
             \ifnum\putbox@remlines>0%
             \else\hangindent=0pt\hangafter=1\let\par=\endgraf\fi}}